import time

from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel
from forms import 主窗口
import os,sys,pymysql,threading
from forms.component.db import 操作_sql
from forms.config import  app_dist,app_info,basefileNames
from forms.forms_产品信息管理 import 产品信息_管理_窗体
from forms.forms_质量标准管理 import 质量标准_管理_窗体
from forms.forms_资料打印管理 import 资料打印_管理_窗体
from forms.component.progressWindow import progress,wrapper_progress
class C_mainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUi()
    def initUi(self):
        self.ui = 主窗口.Ui_MainWindow()
        self.ui.setupUi(self)
        self.statusBarText() #状态栏
        # self.showMenuAtions() #显示菜单栏中的显示控件
        self.menuActionsClickEvents() #菜单单击事件

        # #信号槽
        # print(app_dist.get('账号'))
        self.backup_ui()
        self.updataFilenames()

    def bianliwenj(self,files):
        for root, dirs, files in os.walk(files):
            for file in files:
                basefileNames.append([file, os.path.join(root, file)])

    @wrapper_progress
    def updataFilenames_ui_show(self):

        self.进度条.messageText = '更新数据中...'
        self.进度条.windowTitle = "更新"

        threads=[]
        for file in os.listdir(app_dist['ZJReportPath']):
            if os.path.isfile(os.path.join(app_dist['ZJReportPath'], file)):  # 如果是文件，则导入
                basefileNames.append([file, os.path.join(app_dist['ZJReportPath'], file)])
            else:

                for fil in os.listdir(os.path.join(app_dist['ZJReportPath'], file)):
                    if os.path.isfile(os.path.join(app_dist['ZJReportPath'], file, fil)):  # 如果是文件，则导入
                        basefileNames.append([fil, os.path.join(app_dist['ZJReportPath'], file, fil)])
                    else:

                        for fi in os.listdir(os.path.join(app_dist['ZJReportPath'], file, fil)):
                            if os.path.isfile(os.path.join(app_dist['ZJReportPath'], file, fil, fi)):  # 如果是文件，则导入
                                basefileNames.append([fi, os.path.join(app_dist['ZJReportPath'], file, fil, fi)])
                            else:
                                for f in os.listdir(os.path.join(app_dist['ZJReportPath'], file, fil,fi)):
                                    if os.path.isfile(
                                            os.path.join(app_dist['ZJReportPath'], file, fil, fi,f)):  # 如果是文件，则导入
                                        basefileNames.append(
                                            [f, os.path.join(app_dist['ZJReportPath'], file, fil, fi,f)])
                                    else:
                                        for fa in os.listdir(os.path.join(app_dist['ZJReportPath'], file, fil, fi,f)):
                                            if os.path.isfile(
                                                    os.path.join(app_dist['ZJReportPath'], file, fil, fi,
                                                                 f,fa)):  # 如果是文件，则导入
                                                basefileNames.append(
                                                    [fa, os.path.join(app_dist['ZJReportPath'], file, fil, fi, f,fa)])
                                            else:
                                                for fb in os.listdir(
                                                        os.path.join(app_dist['ZJReportPath'], file, fil, fi, f,fa)):
                                                    if os.path.isfile(
                                                            os.path.join(app_dist['ZJReportPath'], file, fil, fi,
                                                                         f, fa,fb)):  # 如果是文件，则导入
                                                        basefileNames.append(
                                                            [fb,
                                                             os.path.join(app_dist['ZJReportPath'], file, fil, fi, f,
                                                                          fa,fb)])
                                                    else:
                                                        for fc in os.listdir(
                                                                os.path.join(app_dist['ZJReportPath'], file, fil, fi, f,
                                                                             fa,fb)):
                                                            if os.path.isfile(
                                                                    os.path.join(app_dist['ZJReportPath'], file, fil,
                                                                                 fi,
                                                                                 f, fa, fb,fc)):  # 如果是文件，则导入
                                                                basefileNames.append(
                                                                    [fc,
                                                                     os.path.join(app_dist['ZJReportPath'], file, fil,
                                                                                  fi, f,
                                                                                  fa, fb,fc)])
                                                            else:

                                                                thread1 = threading.Thread(
                                                                    target=lambda: self.bianliwenj(
                                                                        os.path.join(app_dist['ZJReportPath'], file, fil, fi,f,fa,fb,fc)))
                                                                # thread1.start()
                                                                threads.append(thread1)

        for t in threads:
            t.start()
        while any(t.is_alive() for t in threads):
            pass

    def updataFilenames(self):
        self.进度条 = progress()
        self.进度条.show()
        thread1 = threading.Thread(target=self.updataFilenames_ui_show)
        thread1.start()

    def backup_ui(self):

        self.进度条 = progress()
        self.进度条.show()
        thread1 = threading.Thread(target=self.backup_ui_show)
        thread1.start()

    @wrapper_progress
    def backup_ui_show(self):
        '''呀处理的事务'''
        self.进度条.messageText = '备份数据中...'
        self.进度条.windowTitle = "更新"

        # 备份数据库
        操作_sql().backup()


    def statusBarText(self):
        self.status_label=QLabel(self)
        self.status_label.setText('安徽嘉佑')
        self.ui.statusBar.addPermanentWidget(self.status_label,1)






    def menuActionsClickEvents(self):
        '''菜单栏单击事件'''

        self.ui.action_CPXX.triggered.connect(self.menu_Click_Event_Func)
        self.ui.action_ZLBZ.triggered.connect(self.menu_Click_Event_Func)
        self.ui.action_DY.triggered.connect(self.menu_Click_Event_Func)

    def menu_Click_Event_Func(self):
        '''菜单栏单击事件的函数'''
        print(self.sender().objectName())
        if self.sender().objectName()=="action_CPXX":
            self.产品信息管理=产品信息_管理_窗体(app=self)
            self.产品信息管理.show()
        elif self.sender().objectName()=="action_ZLBZ":
            self.质量标准管理=质量标准_管理_窗体(app=self)
            self.质量标准管理.show()
        elif self.sender().objectName()=="action_DY":
            print('action_DY',len(basefileNames))
            self.资料打印管理=资料打印_管理_窗体(app=self)
            self.资料打印管理.show()


if __name__=='__main__':
    app = QApplication(sys.argv)
    主窗口1 = C_mainWindow()
    主窗口1.show()

    sys.exit(app.exec())